/*
 * GalleryFloor.fx
 *
 * Created on 2010-01-18, 20:17:32
 */

package galleryviewer;

import javafx.fxd.FXDLoader;
import javafx.scene.input.MouseEvent;
import javafx.scene.*;
import javafx.scene.CustomNode;
import javafx.scene.shape.*;
import javafx.scene.paint.Color;

/**
 * @author m.deluga, jaccus
 */

package var currentFloor : Integer = Constants.INITIAL_FLOOR; //this is how you do something 'static' like

package var floors : GalleryFloor[] = [
	GalleryFloor { floorNumber: 0 mapUrl: "floor0.fxz"}, 
	GalleryFloor { floorNumber: 1 mapUrl: "floor1.fxz"}
];

package var openedPopups: PopUp[];	

public class GalleryFloor {

	public var floorNumber : Integer;
	public var locals : Local[]; // lista lokali tego pietra (jako Node)
	public var galleryContent : Node[];
	public var mapUrl : String; // url do pliku z poziomem galerii
	public var stairsUp : Node[];
	public var stairsDown : Node[];
	public var info : Shape[];
	public var galleryFloorWidth : Number;
	public var galleryFloorHeight : Number;
	
	init {
        var content = FXDLoader.loadContent("{__DIR__}content/{mapUrl}");

        Main.galleryWidth = content.getRoot().boundsInLocal.width;
        Main.galleryHeight = content.getRoot().boundsInLocal.height;
        galleryFloorWidth = content.getRoot().boundsInLocal.width;
        galleryFloorHeight = content.getRoot().boundsInLocal.height;

        //load local objects
        for (i in [0..Constants.LOCALS_AT_FLOOR[floorNumber]]) {
            var localShape : Shape = content.getShape("floor{floorNumber}_{i}_");
            delete localShape from (localShape.parent as Group).content;

            insert Rectangle{x: localShape.boundsInLocal.minX, y: localShape.boundsInLocal.minY, width: localShape.boundsInLocal.width, height: localShape.boundsInLocal.height fill : Color.web("#FF0000") visible : true } into Main.stage.scene.content;

            var local : Local = Local {floor: this node: localShape localId: ++Local.lastLocalId};

            insert local into locals;
            insert local into Local.allLocals;
        }
        
		//load other objects
		for (i in [1..3]) {
		    var goUp = content.getGroup("goUp_{i}");
		    delete goUp from (goUp.parent as Group).content;
		    goUp.onMouseClicked = displayedFloorIncrement;
		    insert goUp into stairsUp;
		    
		    var goDown = content.getNode("goDown_{i}");
		    delete goDown from (goDown.parent as Group).content;
		    insert goDown into stairsDown;
		    
		    var singleInfo = content.getShape("info_{i}");
		    delete singleInfo from (singleInfo.parent as Group).content;
		    insert singleInfo into info;
		}
        
        content =null;
        
        insert [locals,stairsUp,stairsDown,info] into galleryContent;
	}
	public function getLocals():Local[]{
	    return locals;
	}
}

package def getDisplayedLocals = function():Local[]{
    floors[currentFloor].getLocals();
}

package def setDisplayedFloor = function(floorNumber:Integer) {
    if(floorNumber < Constants.FIRST_FLOOR or floorNumber > Constants.LAST_FLOOR) {
        return;
    } 
    currentFloor = floorNumber;
};

package def displayedFloorIncrement = function(e:MouseEvent):Void{
    if(currentFloor < Constants.LAST_FLOOR) {
        setDisplayedFloor(++currentFloor);
        
    }
};

package def displayedFloorDecrement = function(e:MouseEvent):Void{
    if(currentFloor > Constants.FIRST_FLOOR) {
            setDisplayedFloor(--currentFloor);
    }
};
